﻿using GameFramework.DataTable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TowerDefence_Zombie.Data;

namespace TowerDefence_Zombie
{

    public sealed class DailyTaskData
    {
        private DRDailyTaskData dRDailyTaskData;

        private DRDailyTaskAward dRDailyTaskAward;

        public int Id
        {
            get
            {
                return dRDailyTaskData.Id;
            }
        }

        public int[] KillNums
        {
            get
            {
                return dRDailyTaskData.KilllNum;
            }
        }

        public int[] CompleteNums
        {
            get
            {
                return dRDailyTaskData.CompleteNum;
            }
        }

        public int[] BuyBoxs
        {
            get
            {
                return dRDailyTaskData.BuyBoxs;
            }
        }

        public int[] DoubleAtkNums
        {
            get
            {
                return dRDailyTaskData.DoubleAtk;
            }
        }

        public string[] AwardIcon = new string[4];

        public int[] FirstReward
        {
            get
            {
                return dRDailyTaskAward.FirstAward;
            }
        }

        public long[] FirstRewardNum
        {
            get
            {
                return dRDailyTaskAward.FirstAwardNum;
            }
        }

        public int[] SecondReward
        {
            get
            {
                return dRDailyTaskAward.SecondAward;
            }
        }

        public long[] SecondRewardNum
        {
            get
            {
                return dRDailyTaskAward.SecondAwardNum;
            }
        }

        public int[] ThirdReward
        {
            get
            {
                return dRDailyTaskAward.ThirdAward;
            }
        }

        public long[] ThirdRewardNum
        {
            get
            {
                return dRDailyTaskAward.ThirdAwardNum;
            }
        }

        public int[] FourthReward
        {
            get
            {
                return dRDailyTaskAward.FourthAward;
            }
        }

        public long[] FourthRewardNum
        {
            get
            {
                return dRDailyTaskAward.FourthAwardNum;
            }
        }

        public DailyTaskData(DRDailyTaskData dRDailyTaskData,DRDailyTaskAward dRDailyTaskAward)
        {
            this.dRDailyTaskData = dRDailyTaskData;
            this.dRDailyTaskAward = dRDailyTaskAward;
            AwardIcon[0] = dRDailyTaskAward.FirstIcon;
            AwardIcon[1] = dRDailyTaskAward.SecondIcon;
            AwardIcon[2] = dRDailyTaskAward.ThirdIcon;
            AwardIcon[3] = dRDailyTaskAward.FourthIcon;
        }
    }

    public sealed class SingleTaskData
    {
        private DRSignInTask dRSignInTask;

        public int Id
        {
            get
            {
                return dRSignInTask.Id;
            }
        }

        public int DayNum
        {
            get
            {
                return dRSignInTask.DayNum;
            }
        }

        public SingleTaskData(DRSignInTask dRSignInTask)
        {
            this.dRSignInTask = dRSignInTask;
        }
    }

    public sealed class DailyTask : DataBase 
    {
        private IDataTable<DRSignInTask> dtSignInTask;
        private List<SingleTaskData> singleTaskDatas;

        private IDataTable<DRDailyTaskData> dtDailyTaskData;
        private List<DailyTaskData> dailyTaskDatas;

        private IDataTable<DRDailyTaskAward> dtDailyTaskAward;
        private Dictionary<int,DRDailyTaskAward> dailyTaskAwards;


        protected override void OnInit()
        {

        }

        protected override void OnPreload()
        {
            LoadDataTable("SignInTask");
            LoadDataTable("DailyTaskData");
            LoadDataTable("DailyTaskAward");
        }

        protected override void OnLoad()
        {
            base.OnLoad();
            dtSignInTask = GameEntry.DataTable.GetDataTable<DRSignInTask>();
            if (dtSignInTask == null)
            {
                throw new Exception("Can not get SignInTask data table.");
            }

            dtDailyTaskData = GameEntry.DataTable.GetDataTable<DRDailyTaskData>();
            if (dtDailyTaskData == null)
            {
                throw new Exception("Can not get DailyTaskData data table.");
            }


            dtDailyTaskAward = GameEntry.DataTable.GetDataTable<DRDailyTaskAward>();
            if (dtDailyTaskAward == null)
            {
                throw new Exception("Can not get DailyTaskAward data table.");
            }

            dailyTaskAwards = new Dictionary<int, DRDailyTaskAward>();
            foreach (DRDailyTaskAward dRDailyTaskAward in dtDailyTaskAward.GetAllDataRows())
            {
                dailyTaskAwards.Add(dRDailyTaskAward.Id, dRDailyTaskAward);
            }

            singleTaskDatas = new List<SingleTaskData>();
            foreach (DRSignInTask dRSignInTask in dtSignInTask.GetAllDataRows())
            {
                singleTaskDatas.Add(new SingleTaskData(dRSignInTask));
            }

            dailyTaskDatas = new List<DailyTaskData>();
            foreach (DRDailyTaskData dRDailyTaskData in dtDailyTaskData.GetAllDataRows())
            {
                dailyTaskDatas.Add(new DailyTaskData(dRDailyTaskData, dailyTaskAwards[dRDailyTaskData.AwardId]));
            }
        }


        public bool TryNxetSingleTask(int stage, out SingleTaskData singleTaskData)
        {
            singleTaskData = null;
            if (stage >= singleTaskDatas.Count)
                return false;
            singleTaskData = singleTaskDatas[stage];
            return true;
        }


        public List<SingleTaskData> GetAllSingleTask()
        {
            return singleTaskDatas;
        }

        public DailyTaskData GetDailyTaskData(int Day)
        {
            if (Day >= dailyTaskDatas.Count)
                return dailyTaskDatas[^1];
            return dailyTaskDatas[Day];
        }


        public void GetDailyTaskData(int Day,  int taskId, out int[] taskNums)
        {
            DailyTaskData dailyTaskData = null;
            taskNums = null;
            if (Day >= dailyTaskDatas.Count)
                dailyTaskData = dailyTaskDatas[^1];
            else
                dailyTaskData = dailyTaskDatas[Day];

            if (taskId == 1)
                taskNums = dailyTaskData.KillNums;
            else if (taskId == 2)
                taskNums = dailyTaskData.CompleteNums;
            else if (taskId == 3)
                taskNums = dailyTaskData.BuyBoxs;
            else if (taskId == 4)
                taskNums = dailyTaskData.DoubleAtkNums;
        }


        protected override void OnUnload()
        {
            base.OnUnload();
            GameEntry.DataTable.DestroyDataTable<DRSignInTask>();
            GameEntry.DataTable.DestroyDataTable<DRDailyTaskData>();
            GameEntry.DataTable.DestroyDataTable<DRDailyTaskAward>();
            singleTaskDatas = null;
            dailyTaskDatas = null;
            dailyTaskAwards = null;
        }

        protected override void OnShutdown()
        {
            base.OnShutdown();
        }


    }
}
